//
//  HICComposeToolbar.swift
//  vibo
//
//  Created by jht-seven on 16/7/3.
//  Copyright © 2016年 jht-seven. All rights reserved.
//

import UIKit

// toobar 按钮枚举
enum HICComposeToolbarType: Int{
    //图片
    case Picture = 10
    //@
    case Mention = 20
    //#
    case Trend = 30
    //表情
    case Emotion = 40
    //+
    case Add = 50
}


class HICComposeToolbar: UIView {
    
    // 切换键盘按钮
    var emoticonButton: UIButton?
    // 供外界设置
    /*
     如果isEmoticon == true 代表显示表情键盘    button显示image 显示键盘
     如果isEmoticon == false 代表显示的系统键盘 button显示image 显示 笑脸
     */
    var isEmoticon:Bool = false{
        didSet{
            // 显示键盘
            if isEmoticon {
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else {
                // 显示笑脸
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    
    //告知外界按钮点击了
    var closure:((type: HICComposeToolbarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        let pictureButton = addChildButtons("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background", type: .Trend)
         emoticonButton = addChildButtons("compose_emoticonbutton_background", type: .Emotion)
        let addButton = addChildButtons("compose_add_background", type: .Add)
        
        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        closure?(type:HICComposeToolbarType(rawValue: button.tag)!)
    }
    
    //创建button按钮的公共方法
    func addChildButtons(imgName: String, type: HICComposeToolbarType)->UIButton{
        let button = UIButton()
        
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        
        return button
        
        
    }
    
    
}
